<template>
  <!-- 注册组件 -->

  <!-- 饿了么表单 -->
  <el-form :model="form" :rules="rules" ref="form" class="form">
    <!-- 用户名/手机 -->
    <el-form-item class="form-item" prop="username">
      <el-input v-model="form.username" placeholder="用户名/手机号码"></el-input>
    </el-form-item>
    <!-- 验证码 -->
    <el-form-item class="form-item" prop="captcha">
      <el-input v-model="form.captcha" placeholder="验证码">
        <!-- 发送验证码结构 -->
        <template slot="append">
          <el-button type="primary" @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>
    <!-- 昵称 -->
    <el-form-item class="form-item" prop="nickname">
      <el-input v-model="form.nickname" placeholder="你的名字"></el-input>
    </el-form-item>
    <!-- 密码 -->
    <el-form-item class="form-item" prop="password">
      <el-input type="password" v-model="form.password" placeholder="密码"></el-input>
    </el-form-item>
    <!-- 确认密码 -->
    <el-form-item class="form-item" prop="checkPassword">
      <el-input type="password" v-model="form.checkPassword" placeholder="确认密码"></el-input>
    </el-form-item>
    <!-- 按钮 -->
    <el-form-item>
      <el-button type="primary" class="submit" @click="handleRegSubmit">注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  //数据
  data() {
    //value是调用者，这里指form.checkPassword
    let checkPassword = (rule, value, callback) => {
      if (value == "") {
        return callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        return callback(new Error("两次输入的密码不一样"));
      } else {
        callback();
      }
    };
    return {
      //表单数据
      form: {
        username: "",
        //验证码
        captcha: "",
        nickname: "",
        password: "",
        checkPassword: ""
      },

      //规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        checkPassword: [{ validator: checkPassword, trigger: "blur" }]
      }
    };
  },
  //方法
  methods: {
    //发送验证码
    handleSendCaptcha() {
      //判断手机号码
      if (this.form.username === "") {
        this.$confirm(`手机号码不能为空`, "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }

      //判断手机号码的长度是不是有11位
      if (this.form.username.length !== 11) {
        this.$confirm("手机号码格式不正确", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      } else {
        this.$axios({
          url: "/captchas",
          method: "post",
          data: { tel: this.form.username }
        }).then(res => {
          console.log(res.data);
          //弹出验证码
          this.$confirm(`模拟手机验证码为：${res.data.code}`, "提示", {
            confirmButtonText: "确定",
            showCancelButton: false,
            //类型：警告
            type: "warning"
          });
        });
      }
    },

    //注册按钮
    handleRegSubmit() {
      console.log(this.form);

      //注册前，先表单全部验证一遍
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log("表单校验成功");
          //发起注册请求

          const { checkPassword, ...data } = this.form;
          this.$axios({
            url: "/accounts/register",
            method: "post",
            data: data
          }).then(res => {
            console.log(res.data);
            if (res.data.token) {
              //注册成功弹框
              this.$message.success("注册成功");
              //注册完毕之后，跳转到登录页
              setTimeout(() => {
                this.$emit("cut");
              }, 800);
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.form {
  padding: 25px;
  .form-item {
    margin-bottom: 20px;
  }
  .form-text {
    font-size: 12px;
    color: #409eff;
    text-align: right;
    line-height: 1;
  }
  .submit {
    width: 100%;
    margin-top: 10px;
  }
}
</style>